/* eslint-disable no-undef */
// 应用设置
import settings from '@/settings'
// 静态菜单
import staticMenu from '@/menus'
import { cloneObj } from '@/utils/data'

const LOCAL_NAME = 'epfrontmenu_' + settings.siteTitle

let { menuType } = settings

// 改动过数据结构，省得让用户做一步清除缓存操作
function getFavouriteLocal() {
  let data = null
  try {
    data = JSON.parse(localStorage.getItem(LOCAL_NAME) || '{}')
    if (typeof data !== 'object' || Array.isArray(data)) {
      data = {}
    }
  } catch (error) {
    data = {}
  }
  return data
}
function getFavouriteFromState(state, loginId) {
  let data = {}
  if (typeof state.menuFavourite === 'object' || !Array.isArray(state.menuFavourite)) {
    data = cloneObj(state.menuFavourite)
  }
  if (!Object.hasOwnProperty.call(data, loginId)) {
    data[loginId] = []
  }
  return data
}

const state = () => ({
  menuJSON: menuType === 'static' ? cloneObj(staticMenu) : [],
  menuFavourite: getFavouriteLocal(),
  menuIsNarrow: true
})
// getters
const getters = {
  getMyFavouriteMenu: (state, getters, rootState) => {
    let loginId = rootState.app.userMainInfo.loginId
    return state.menuFavourite[loginId] || []
  }
}

// actions
const actions = {
  setMenuFavourite({ state, commit, rootState }, { menuItem, groupName }) {
    commit('SETMENUFAVOURITE', { menuItem, groupName, loginId: rootState.app.userMainInfo.loginId })
  },
  delMenuFavourite({ state, commit, rootState }, menuItem) {
    commit('DELMENUFAVOURITE', { menuItem, loginId: rootState.app.userMainInfo.loginId })
  },
  cleanMenuFavourite({ state, commit, rootState }) {
    commit('CLEANMENUFAVOURITE', { loginId: rootState.app.userMainInfo.loginId })
  },
  setMenuFavouriteOrder({ state, commit, rootState }, val) {
    commit('SETMENUFAVOURITEORDER', { loginId: rootState.app.userMainInfo.loginId, newOrder: val })
  },
  setMenuJSON({ state, commit, rootState }, val) {
    commit('SETMENUJSON', val)
  },
  setMenuJSONStatic({ state, commit, rootState }, val) {
    commit('SETMENUJSONSTATIC', { data: val, userInfo: rootState.app.userMainInfo })
  },
  setChangeWidth({ commit }, val) {
    commit('SETCHANGEWIDTH', val)
  }
}

// mutations
const mutations = {
  SETMENUJSONSTATIC(state, { data, userInfo }) {
    state.menuJSON = staticMenu
    // 简单的按用户某个字段进行路由/菜单鉴权
    // state.menuJSON = staticMenu.filter(v => v.userLevel  === '1')
  },
  SETMENUJSON(state, data) {
    state.menuJSON = data
  },
  SETMENUFAVOURITE(state, { menuItem, groupName, loginId }) {
    let newObj = getFavouriteFromState(state, loginId)
    let data = newObj[loginId]
    let index = data.findIndex(item => item.index === menuItem.index)
    if (index === -1) {
      if (groupName) {
        menuItem['groupName'] = groupName
      }
      data.push(menuItem)
    }
    // debugger
    // data = removeUnExistMenu(data, state.routerJson)
    state.menuFavourite = newObj
    localStorage.setItem(LOCAL_NAME, JSON.stringify(state.menuFavourite))
  },
  DELMENUFAVOURITE(state, { menuItem, loginId }) {
    // 改了数据结构了 判断下
    let newObj = getFavouriteFromState(state, loginId)
    let data = newObj[loginId]

    let index = data.findIndex(item => item.index === menuItem.index)
    if (index !== -1) {
      data.splice(index, 1)
    }
    // data = removeUnExistMenu(data, state.routerJson)
    state.menuFavourite = newObj
    localStorage.setItem(LOCAL_NAME, JSON.stringify(state.menuFavourite))
  },
  CLEANMENUFAVOURITE(state, { loginId }) {
    let newObj = getFavouriteFromState(state, loginId)
    delete newObj[loginId]
    state.menuFavourite = newObj
    localStorage.setItem(LOCAL_NAME, JSON.stringify(state.menuFavourite))
  },
  SETMENUFAVOURITEORDER(state, { newOrder, loginId }) {
    let newObj = getFavouriteFromState(state, loginId)
    newObj[loginId] = newOrder
    state.menuFavourite = newObj
    localStorage.setItem(LOCAL_NAME, JSON.stringify(state.menuFavourite))
  },
  SETCHANGEWIDTH(state, data) {
    state.menuIsNarrow = data
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
